package com.cx.algrithm.queue.common;

public class CycleQueue{

    private int maxSize; // 数组最大容量
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr; //队列存放

    public CycleQueue(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

    public boolean isFull(){
        return (rear+1)%maxSize==front;
    }

    public boolean isEmpty(){
        return rear == front;
    }

    public void addQueue(int n){

        if(isFull()){
            System.out.println("队列满了");
            return;
        }
        arr[rear] = n;
        rear = (rear+1)%maxSize;
    }

    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    public void showQueue(){
        if(isEmpty()){
            System.out.println("队列空");
            return;
        }
        for(int i = front;i<front+size();i++){
            System.out.printf("arr[%d]=%d\n",i%maxSize,arr[i%maxSize]);
        }
    }

    public int size(){
        return (rear + maxSize - front) % maxSize;
    }

    public int headQueue(){
        if(isEmpty()){
            throw new RuntimeException("队列空");
        }
        return arr[front];
    }
}
